<template>
  <div class="log_detail" v-if="dialogViewLog">
    <el-dialog
      title="操作记录"
      v-dialogDrag
      :close-on-click-modal="false"
      width="1200px"
      :visible.sync="dialogViewLog"
      customClass="mes_dialog"
      @closed="clearData">
      <div class="table_container" v-loading="isLoading">
        <div class="viewName" v-if="number && name">{{number+'：'+name}}</div>
        <el-table
          border
          stripe
          :max-height="maxHeight"
          :data="logList"
          tooltip-effect="dark"
          class="log_detail_table"
          @sort-change="_sort">
          <el-table-column
            prop="changeDate"
            label="变更时间"
            sortable="custom"
            show-overflow-tooltip
            align="center"
            width="170">
          </el-table-column>
          <el-table-column
            prop="description"
            label="变更内容"
            className="break_left"
            min-width="350">
            <template slot-scope="scope">
              {{scope.row.description}}
              <span
                class="appendix_file"
                @click="$downByBlob(scope.row.uploadHistoryId, scope.row.fileName)"
                v-if="scope.row.uploadHistoryId">附件: {{scope.row.fileName}}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="changerName"
            label="变更人"
            show-overflow-tooltip
            align="center"
            min-width="100">
          </el-table-column>
          <el-table-column
            :resizable="false"
            v-if="!noRemark"
            prop="reason"
            label="备注"
            className="break_left"
            show-overflow-tooltip
            min-width="300">
          </el-table-column>
        </el-table>
        <slot></slot>
      </div>
      <span slot="footer" class="mes_btn">
        <el-button @click="dialogViewLog = false">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'LogDetail',
  props: {
    maxHeight: {
      type: String,
      default: '440'
    }
  },
  data() {
    return {
      // 是否显示
      dialogViewLog: false,
      // 加载中
      isLoading: false,
      // 编码
      number: '',
      // 名称
      name: '',
      // 日志列表
      logList: [],
      // 是否显示备注
      noRemark: false,
      // 当前请求对象
      curData: {},
      // 排序
      sortData: {}
    }
  },
  methods: {
    /**
     * 打开方法
     * @param obj
     * title: 标题，String，必传，示例“工单编号”
     * name: 名称，String，必传，示例“GD-001”
     * noRemark: 不显示备注，非必传，默认为false
     * url: 接口地址，String，必传，示例“a/log/log/list”
     * data: 接口参数，Object，和query二选一，示例{}
     * query: 接口参数，Object，和data二选一，示例{}
     */
    openDialog(obj) {
      this.number = obj.title || ''
      this.name = obj.name || ''
      this.noRemark = obj.noRemark || false
      this.curData = {}
      this.init(obj)
      this.dialogViewLog = true
    },
    // 初始化
    init(obj) {
      if (obj && !obj.url) return
      if (obj) this.curData = obj
      this.isLoading = true
      let postData = this.curData.data || {
        pageSize: 9999,
        pageNo: 9999,
        query: this.curData.query || {}
      }
      if (Object.keys(this.sortData).length > 0) {
        postData.sort = this.sortData
      }
      this.$store.dispatch('saveData', {
        url: this.curData.url,
        method: 'post',
        data: postData,
        noCommit: true
      }).then(res => {
        this.logList = Array.isArray(res.data) ? res.data : res.data.list || []
      }).catch(() => {
        this.logList = []
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 排序
    _sort(obj) {
      this.sortData = this.$getSortOrder(obj)
      this.init()
    },
    // 关闭弹窗清除数据
    clearData() {
      this.logList = []
      this.curData = {}
    },
  }
}
</script>

<style lang="less">
  .log_detail {
    .viewName {
      padding-bottom: 10px;
    }
  }

  .log_detail_table {
    .el-table__body-wrapper {
      td.break_left {
        .cell {
          height: unset;
          white-space: normal;
          line-height: 18px;
          padding: 3px 10px;
          word-break: break-all;
        }
        .appendix_file{
          padding-left: 4px;
          color:#00b4ff;
          cursor: pointer;
        }
      }
    }
  }
</style>
